﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace SPDbFileExtractor.Framework.Code
{
    public static class Utils
    {
        /// <summary>
        /// Shows the message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void ShowMessage(string message)
        {
            MessageBox.Show(message, @"Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Shows the error.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void ShowWinFormError(string message)
        {
            MessageBox.Show(message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// Shows the error.
        /// </summary>
        /// <param name="e">The e.</param>
        public static void ShowWinFormError(Exception e)
        {
            ShowWinFormError(e == null ? "N/A" : e.Message);
        }

        /// <summary>
        /// Saves the requested file.
        /// </summary>
        /// <param name="fileId">The file id.</param>
        /// <param name="version">The version.</param>
        /// <param name="connectionString"> </param>
        /// <param name="fileName"> </param>
        public static void SaveRequestedFile(Guid fileId, int version, string connectionString, string fileName)
        {
            var fileContent = ContentDbManager.GetItemContent(connectionString, fileId, version);

            if (fileContent == null)
            {
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.OverwritePrompt = true;
            sfd.CheckPathExists = true;
            sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            sfd.Title = "Choose save location for extracted file...";
            sfd.FileName = fileName;
            sfd.SupportMultiDottedExtensions = true;
            //sfd.Filter = "Extracted file|*" + Path.GetExtension(fileName);

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllBytes(sfd.FileName, fileContent.Content);
            }
        }

        /// <summary>
        /// Validates the connection string.
        /// </summary>
        /// <param name="connectionString">To conn. string.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public static bool ValidateConnectionString(string connectionString)
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    conn.Close();

                    ConnectionManager.ConnectionValid = true;

                    return true;
                }
            }
            catch
            {
                ConnectionManager.ConnectionValid = false;

                return false;
            }
        }
    }
}
